#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define DEV0_FILE "/dev/led_gpio_5_0"
#define DEV1_FILE "/dev/led_gpio_1_1"
#define DEV2_FILE "/dev/led_gpio_0_13"

int main(int argc, char *argv[])
{
	int i, fd;
	int cmd;

	if (-1 == fd0 || -1 == fd1 || -1 == fd2) {
		perror("Error opening device file");
		return -1;
	} else {
		printf("Successfully opened!\n");
	}

	// 0 - DEV0 On
	// 1 - DEV0 Off
	// 2 - DEV1 On
	// 3 - DEV1 Off
	// 4 - DEV2 On
	// 5 - DEV2 Off

	if (argc > 2) {
		if (strcmp(argv[2], "on") == 0) {
			cmd = 0;
		} else if (strcmp(argv[2], "off") == 0) {
			cmd = 1;
		} else {
			printf("Invalid action: %s\n", argv[2]);
			return 0;
		}

		if (strcmp(argv[1], "red") == 0) {
			cmd += 4;
			fd = open(DEV0_FILE, O_WRONLY);
		} else if (strcmp(argv[1], "green") == 0) {
			cmd += 2;
			fd = open(DEV1_FILE, O_WRONLY);
		} else if (strcmp(argv[1], "blue") == 0) {
			cmd += 0;
			fd = open(DEV2_FILE, O_WRONLY);
		} else {
			printf("Invalid color: %s\n", argv[1]);
			return 0;
		}
		printf("Command: %d\n", cmd);
		
		write(fd, (void *) &cmd, sizeof (int));
	} else {
		printf("Usage: chr_dev_app <color> <on/off>\n");
		for (i = 0; i < 100; i++) {
			cmd = i % 6;
			printf("Command: %d\n", cmd);

			fd = get_device_file(cmd / 2);
			write(fd, (void *) &cmd, sizeof (int));

			sleep(1);
		}
	}

	close(fd);

	return 0;
}

